package com.xjnt.mnui.service;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xjnt.frameworks.annotation.Service;
import com.xjnt.frameworks.core.BaseService;
import com.xjnt.mnui.entity.SubjectAssets;

/** 
* 类说明：资产管理service类
* @author 作者 LUXIN
* @version 创建时间：2015年11月19日 上午11:06:27 
*/
@Service
public class SubjectAssetsService extends BaseService<SubjectAssets>{

	@Override
	public SubjectAssets getModel() {
		return SubjectAssets.me;
	}
	/**
	 * 获取资产list
	 * 选择所有sub_uuid is null 的资产数据
	 * @param subjectId
	 * @return list
	 */
	public List<Record> findAssetsList(String subjectId){
		String sql = "";
		List<Record> assetsList = new ArrayList<Record>();
		if(subjectId == null){
			//add页面
			sql = "SELECT a.uuid, a.NAME, ( SELECT s.uuid FROM mnui_subject s WHERE s.uuid = a.sub_uuid ) AS 'selected' FROM mnui_subject_assets a where a.sub_uuid is null and a.type = ?";
			assetsList = Db.find(sql, new Object[]{"0"});
		}else{
			//edit页面
			sql = "SELECT a.uuid,a.NAME,a.sub_uuid as 'selected' from mnui_subject_assets a where (a.sub_uuid is null or a.sub_uuid = ?) and a.type = ?";
			assetsList = Db.find(sql, new Object[]{subjectId,"0"});
		}
		return assetsList;
	}
	
	/**
	 * 通过专题id获取相关资产list
	 * @param subID
	 * @return List<SubjectAssets>
	 */
	public List<SubjectAssets> getListBySubId(Object subId){
		String sql = "select a.uuid, a.name, a.url, a.img_url, a.focus_img_url,a.create_time, a.update_time from mnui_subject_assets a where a.sub_uuid = ?";
		List<Record> l = Db.find(sql,new Object[]{subId});
		List<SubjectAssets> list = new ArrayList<SubjectAssets>();
		for (Record record : l) {
			SubjectAssets subjectAssets = new SubjectAssets();
			subjectAssets.set("uuid", record.get("uuid"));
			subjectAssets.set("name", record.get("name"));
			subjectAssets.set("url", record.get("url"));
			subjectAssets.set("img_url", record.get("img_url"));
			subjectAssets.set("focus_img_url", record.get("focus_img_url"));
			subjectAssets.set("create_time", record.get("create_time"));
			subjectAssets.set("update_time", record.get("update_time"));
			list.add(subjectAssets);
		}
		return list;
	}
}
 